package tech.aistar.corejava.day15;

/**
 * Created by 豆梦寒 on 2019/8/3.
 */
public class ThreadAndRunable {
    public static void main(String[] args) {
        Thread t1 = new T1();
        Thread t2 = new T1();

        t1.setName("window01");
        t2.setName("window02");

        t1.start();
        t2.start();

        Runnable r = new T2();
        Thread t3 = new Thread(r);
        Thread t4 = new Thread(r);

        t3.start();
        t4.start();


    }
}

class T1 extends Thread{
    //资源
    //private int ticket = 10; //卖出10张票
    //静态资源

    private static int ticket = 10;
    //线程不安全的对象尽量不要设置成Static
    private StringBuilder builder= new StringBuilder();

    private static StringBuffer buffer = new StringBuffer();

    public void  run(){
        for (int i = 0; i < 100000; i++) {
            System.out.println(Thread.currentThread().getName()+"正在买票，还剩余"+ticket+"票");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
class T2 implements Runnable{
    //资源...
    private int ticket = 10;//卖票
    @Override
    public void run() {

        for (int i = 0; i < 10000; i++) {
            System.out.println(Thread.currentThread().getName()+"正在买票，还剩余"+ticket+"票");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}